import numpy as np
import pandas as pd
import scanpy as sc
sc.settings.verbosity = 3
sc.logging.print_header()
sc.settings.set_figure_params(dpi=80, facecolor='white')
save_dir='./' ## need to change the directory
sc.settings.figdir = save_dir
The data could be downloaded from https://drive.google.com/drive/folders/1MZEaM28PHW4FTSKzy-h_OqPnSlRKiYKW?usp=sharing.
%%time
adata=sc.read('./Hochgerner_dentate_gyrus_QC.h5ad') ## need to change the directory
np.unique(adata.obs['CellTypes'])
len(np.unique(adata.obs['CellTypes']))
adata
Note: the data is already filtered after proper quality control.
sc.pp.normalize_total(adata, target_sum=1e4)
sc.pp.log1p(adata)
sc.pp.highly_variable_genes(adata,
n_top_genes=3000
)
print(sum(adata.var.highly_variable))
sc.pl.highly_variable_genes(adata)
use_highly_variable=True
from sklearn.preprocessing import StandardScaler
expr = adata[:, adata.var['highly_variable']].X if use_highly_variable else adata.X
expr=StandardScaler(with_mean=False).fit_transform(expr)
expr[expr > 10] = 10
from sklearn.decomposition import TruncatedSVD
transformer = TruncatedSVD(n_components=50, random_state=42)
adata.obsm['X_pca']= transformer.fit_transform(expr)
sc.pl.pca(adata,color='CellTypes')
%%time
sc.tl.dendrogram(adata,groupby='CellTypes',use_rep='X_pca')
celltype=adata.obs['CellTypes'].values.copy()
new_order=adata.uns["dendrogram_['CellTypes']"]['categories_ordered']
celltype=celltype.reorder_categories(new_order)
adata.obs['CellTypes']=celltype
%%time
sc.pp.neighbors(adata,
n_neighbors=15,random_state=10,knn=True,
method="umap")
%%time
sc.tl.umap(adata)
%%time
sc.pl.umap(adata,
color=['CellTypes'],
palette=sc.pl.palettes.default_102,
size=10,
frameon=True)
Import COSG:
import cosg as cosg
import importlib
importlib.reload(cosg)
Print the usgae:
help(cosg.cosg)
%%time
import time
t0= time.clock()
cosg.cosg(adata,
key_added='cosg',
mu=1,
n_genes_user=50,
groupby='CellTypes')
runtime_cosg = time.clock() - t0
sc.tl.dendrogram(adata,groupby='CellTypes',use_rep='X_pca')
sc.pl.rank_genes_groups_dotplot(adata,groupby='CellTypes',
cmap='Spectral_r',
standard_scale='var',
n_genes=3,key='cosg')
%%time
t0= time.clock()
sc.tl.rank_genes_groups(adata,
groupby='CellTypes',
method='logreg',
key_added='logreg',
n_genes=50)
runtime_logreg = time.clock() - t0
sc.pl.rank_genes_groups_dotplot(adata,groupby='CellTypes',
cmap='Spectral_r',
standard_scale='var',
n_genes=3,key='logreg')
%%time
t0= time.clock()
sc.tl.rank_genes_groups(adata, groupby='CellTypes',
tie_correct=False,
method='wilcoxon',
key_added='wilcoxon',n_genes=50)
runtime_wilcoxon= time.clock() - t0
sc.pl.rank_genes_groups_dotplot(adata,groupby='CellTypes',
cmap='Spectral_r',
standard_scale='var',
n_genes=3,key='wilcoxon')
%%time
t0= time.clock()
sc.tl.rank_genes_groups(adata, groupby='CellTypes',
tie_correct=True,
method='wilcoxon',
key_added='wilcoxon_tie',n_genes=50)
runtime_wilcoxon_tie = time.clock() - t0
sc.pl.rank_genes_groups_dotplot(adata,groupby='CellTypes',
cmap='Spectral_r',
standard_scale='var',
n_genes=3,key='wilcoxon_tie')
print( [runtime_logreg, runtime_wilcoxon, runtime_wilcoxon_tie, runtime_cosg] )
celltype_selected= 'GC-adult'
adata_selected=adata[adata.obs['CellTypes'].isin(['CA3-Pyr', 'GC-adult', 'GC-juv'])]
marker_violin=np.hstack([pd.DataFrame(adata.uns['logreg']['names'])[celltype_selected][:3].values,
pd.DataFrame(adata.uns['wilcoxon']['names'])[celltype_selected][:3].values,
pd.DataFrame(adata.uns['wilcoxon_tie']['names'])[celltype_selected][:3].values,
pd.DataFrame(adata.uns['cosg']['names'])[celltype_selected][:3].values])
marker_violin
vp=sc.pl.stacked_violin(adata_selected,
marker_violin,
groupby='CellTypes',
swap_axes=True,
figsize=(4, 9),
scale='width',
yticklabels=True,
size=0.2,
inner='box',
cut=True,
return_fig=True,
)
icolor=[ "#ffe119", # Logistic regression
"#e6194b", # Wilcoxon-test
"#4363d8", # Wilcoxon-test (TIE)
"#3cb44b" # COSG ,
]
icolor=np.hstack([np.repeat(i,3) for i in icolor])
icolor
icolor=list(icolor)
vp.style(ylim=(0,6),
row_palette=icolor,yticklabels=True,).show()
adata.write(save_dir+'/COSG_tutorial_Hochgerner_dentate_gyrus_QC.h5ad')
Min Dai, daimin@zju.edu.cn